package org.example.demo1.controller;


import org.example.demo1.ai.ChatRedisMemory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class RedisAiController {

    @Autowired
    ChatClient chatClient;

    @Autowired
    ChatRedisMemory chatRedisMemory;


    /**
     * 持久化聊天记录
     * @param message
     * @param sessionId
     * @return
     */
    @GetMapping("/ai/redisCall")
    public String redisCall(@RequestParam(value = "message", defaultValue = "讲个笑话") String message, @RequestParam String sessionId) {
        return chatClient.prompt().user(message)
                .advisors(new MessageChatMemoryAdvisor(chatRedisMemory, sessionId, 10))
                .call().content().trim();
    }
}
